package live.ifox.helper;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PackageHelper {
    private PackageManager packageManager;
    private PackageInfo packageInfo;
    private ApplicationInfo applicationInfo;
    private List<Map<String, Object>> infoModel;
    private Context mContext;

    public PackageHelper(Context context) {
        mContext = context;
        infoModel = new ArrayList<>();
        try {
            packageManager = context.getPackageManager();
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            applicationInfo = packageInfo.applicationInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    boolean isAppInstalled(String pkgName) {
        if (pkgName == null || pkgName.isEmpty())
            return false;

        List<PackageInfo> pkgList = packageManager.getInstalledPackages(0);
        if (pkgList.isEmpty())
            return false;

        for (PackageInfo item : pkgList) {
            if (pkgName.equals(item.packageName))
                return true;
        }
        return false;
    }

    private void addItem(String label, Object detail) {
        Map<String, Object> map = new HashMap<>();
        map.put("label", label);
        map.put("detail", detail);
        infoModel.add(map);
    }

    public String getPackageName() {
        return packageInfo.packageName;
    }

    public String getVersionName() {
        return packageInfo.versionName;
    }

    public long getVersionCode() {
        return packageInfo.versionCode;
    }

    public String getAppName() {
        return applicationInfo.loadLabel(packageManager).toString();
    }

    // 资源路径  应用APK的全路径
    public String getSourceDir() {
        return applicationInfo.sourceDir;
    }

    public String getDataDir() {
        return applicationInfo.dataDir;
    }

    // 本地路径  JNI本地库存放路径 applicationInfo.nativeLibraryDir;

    public String getProcessName() {
        return applicationInfo.processName;
    }

    public List<Map<String, Object>> getModel() {
        addItem("uid", applicationInfo.uid);
        addItem("packageName", packageInfo.packageName);
        addItem("versionName", packageInfo.versionName);
        addItem("versionCode", packageInfo.versionCode);
        addItem("appName", applicationInfo.loadLabel(packageManager).toString());
        addItem("processName", applicationInfo.processName);
        addItem("targetSdkVersion", applicationInfo.targetSdkVersion);
        // addItem("sourceDir", applicationInfo.sourceDir);
        // addItem("dataDir", applicationInfo.dataDir);
        // addItem("publicSourceDir", applicationInfo.publicSourceDir);
        // addItem("nativeLibraryDir", applicationInfo.nativeLibraryDir);

        addItem("context.Cache", mContext.getCacheDir().getAbsolutePath());
        // addItem("Databases",mContext.getDataDir().getAbsolutePath());
        addItem("context.Files", mContext.getFilesDir().getAbsolutePath());
        addItem("context.Obb", mContext.getObbDir().getAbsolutePath());
        return infoModel;
    }
}
